package com.wyq.service.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.wyq.bean.Audience;
import com.wyq.dao.AudienceDao;
import com.wyq.service.AudienceService;

public class AudienceServiceImpl implements AudienceService {
	
	private AudienceDao audienceDao;
	
	public AudienceDao getAudienceDao() {
		return audienceDao;
	}
	
	public void setAudienceDao(AudienceDao audienceDao) {
		this.audienceDao = audienceDao;
	}
	
	public boolean existAudience(Map<String, String> options) {
		// TODO Auto-generated method stub
		return audienceDao.existAudience(options);
	}

	public List<Audience> findPageAudiences(String start,String limit,String type) throws Exception {
		// TODO Auto-generated method stub
		return audienceDao.findPageAudiences(start,limit,type);
	}
	
	public List<Audience> findAllAudiences(String type) throws Exception {
		// TODO Auto-generated method stub
		return audienceDao.findAllAudiences(type);
	}

	public Audience findAudienceById(int id) {
		// TODO Auto-generated method stub
		return audienceDao.findAudienceById(id);
	}

	public void removeAudience(Audience audience) {
		// TODO Auto-generated method stub
		audienceDao.removeAudience(audience);
	}

	public void saveAudience(Audience audience) {
		// TODO Auto-generated method stub
		audienceDao.saveAudience(audience);
	}

	public void updateAudience(Audience audience) {
		// TODO Auto-generated method stub
		audienceDao.updateAudience(audience);
	}


	public List findAudienceIsRegister(String name, String value,String showInfoId)
			throws Exception {
		return audienceDao.findAudienceIsRegister(name, value,showInfoId);
	}

	public List getAudienceRegisterInfo(Audience audience){
		return audienceDao.getAudienceRegisterInfo(audience);
	}
	
	//得到数据库中用户最大的ID
	public List getAudienceMaxID() throws Exception{
		return audienceDao.getAudienceMaxID();
	}
	
	//得到用户的调查问卷信息
	public List getAudienceQInfo(String audienceId) throws Exception{
		return audienceDao.getAudienceQInfo(audienceId);
	}
	
	//生成xsl
	@SuppressWarnings("deprecation")
	public InputStream getInputStream(String showInfoId) throws Exception {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("sheet1");
		sheet.setDefaultColumnWidth((short)15);
		
		HSSFRow row = sheet.createRow(0);
		
		HSSFCell cell = row.createCell((short) 0);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("编号");
		
		cell = row.createCell((short) 1);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("姓名");
		
		cell = row.createCell((short) 2);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("性别");
		
		cell = row.createCell((short) 3);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("公司");
		
		cell = row.createCell((short) 4);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("部门");
		
		cell = row.createCell((short) 5);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("职位");
		
		cell = row.createCell((short) 6);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("地址1");
		
		cell = row.createCell((short) 7);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("地址2");
		
		cell = row.createCell((short) 8);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("国家");
		
		cell = row.createCell((short) 9);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("省份");
		
		cell = row.createCell((short) 10);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("城市");
		
		cell = row.createCell((short) 11);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("邮编");
		
		cell = row.createCell((short) 12);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("电话");
		
		cell = row.createCell((short) 13);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("传真");
		
		cell = row.createCell((short) 14);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("手机"); 
		
		cell = row.createCell((short) 15);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("邮箱");
		
		cell = row.createCell((short) 16);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("网址");
		
		cell = row.createCell((short) 17);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("登记日期");
		
		cell = row.createCell((short) 18);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("是否可用");
		
		cell = row.createCell((short) 19);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("展会名称");
		
		List list = audienceDao.findAllAudiences(showInfoId);
		
		for(int i=0;i<list.size();i++){
			Map audience = (Map) list.get(i);
			
			row = sheet.createRow(i+1);
			
			cell = row.createCell((short) 0);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(i+1);
			
			cell = row.createCell((short) 1);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(audience.get("audience_name") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(audience.get("audience_name").toString());
			}
			
			cell = row.createCell((short) 2);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(audience.get("audience_sex") == null){
				cell.setCellValue("");
			}else{
				if(null != audience.get("audience_sex")){
					String audience_sex = audience.get("audience_sex").toString();
					if("0".equals(audience_sex)){
						cell.setCellValue("先生");
					}else if("1".equals(audience_sex)){
						cell.setCellValue("女士");
					}
				}
			}
			
			cell = row.createCell((short) 3);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(audience.get("audience_company") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(audience.get("audience_company").toString());
			}
			
			cell = row.createCell((short) 4);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(audience.get("audience_department") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(audience.get("audience_department").toString());
			}
			
			cell = row.createCell((short) 5);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(audience.get("audience_position") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(audience.get("audience_position").toString());
			}
			
			cell = row.createCell((short) 6);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(audience.get("audience_address1") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(audience.get("audience_address1").toString());
			}
			
			cell = row.createCell((short) 7);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(audience.get("audience_address2") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(audience.get("audience_address2").toString());
			}
			
			cell = row.createCell((short) 8);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(audience.get("audience_nation") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(audience.get("audience_nation").toString());
			}
			
			cell = row.createCell((short) 9);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(audience.get("audience_province") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(audience.get("audience_province").toString());
			}
			
			cell = row.createCell((short) 10);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(audience.get("audience_city") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(audience.get("audience_city").toString());
			}
			
			cell = row.createCell((short) 11);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(audience.get("audience_postcode") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(audience.get("audience_postcode").toString());
			}
			
			cell = row.createCell((short) 12);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(audience.get("audience_tel") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(audience.get("audience_tel").toString());
			}
			
			cell = row.createCell((short) 13);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(audience.get("audience_fax") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(audience.get("audience_fax").toString());
			}
			
			cell = row.createCell((short) 14);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(audience.get("audience_mobile") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(audience.get("audience_mobile").toString());
			}
			
			cell = row.createCell((short) 15);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(audience.get("audience_email") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(audience.get("audience_email").toString());
			}
			
			cell = row.createCell((short) 16);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(audience.get("audience_weburl") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(audience.get("audience_weburl").toString());
			}
			
			cell = row.createCell((short) 17);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(audience.get("audience_registerDate") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(audience.get("audience_registerDate").toString());
			}
			
			cell = row.createCell((short) 18);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(audience.get("audience_isAvailable") == null){
				cell.setCellValue("");
			}else{
				String isAvailable = audience.get("audience_isAvailable").toString();
				if(isAvailable.equals("1")){
					cell.setCellValue("是");
				}else{
					cell.setCellValue("否");
				}
			}
			
			cell = row.createCell((short) 19);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			if(audience.get("showName") == null){
				cell.setCellValue("");
			}else{
				cell.setCellValue(audience.get("showName").toString());
			}
			
		}
		
		/*File file = new File("test.xls");
		
		OutputStream os = new FileOutputStream(file);
		
		wb.write(os);
		os.close();
		
		InputStream is = new FileInputStream(file);*/
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		wb.write(os);
		byte[] content = os.toByteArray();
		InputStream is = new ByteArrayInputStream(content);
		
		return is;
	}

	public void update() {
		// TODO Auto-generated method stub
		audienceDao.update();
	}



}
